package com.xiannimei.service.oauth;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiannimei.BusinessException;
import com.xiannimei.model.Platform;

@Service("oauthTencent")
@Transactional
public class OAuthManagerImplTencent extends OAuthManagerAbstractImpl {

	@Value("#{oauth_tencent['appKey']}")
	private String client_id;
	@Value("#{oauth_tencent['appSecret']}")
	private String client_secret;
	@Value("#{oauth_tencent['grant_type']}")
	private String grant_type;
	@Value("#{oauth_tencent['response_type']}")
	private String response_type;
	@Value("#{oauth_tencent['redirect_uri']}")
	private String redirect_uri;
	@Value("#{oauth_tencent['urlAuthorize']}")
	private String urlAuthorize;
	@Value("#{oauth_tencent['urlGetToken']}")
	private String urlGetToken;
	@Value("#{oauth_tencent['scope']}")
	private String scope;

	@Value("#{oauth_tencent['display']}")
	private String display;
	@Value("#{oauth_tencent['format']}")
	private String format;
	@Value("#{oauth_tencent['urlGetOpenId']}")
	private String urlGetOpenId;
	@Value("#{oauth_tencent['urlGetUser']}")
	private String urlGetUser;

	@Override
	public String getLoginUrl(String state) {
		return genericUrl(urlAuthorize, "response_type", response_type, "client_id", client_id, "redirect_uri",
				redirect_uri, "scope", scope, "display", display, "state", state);
	}

	@Override
	public String getNick(JSONObject userJo) {
		if (userJo.has("nickname")) {
			return userJo.getString("nickname");
		} else {
			log.warn("get tencent user error :\n" + userJo);
			throw new BusinessException(E_USERJOERR);
		}
	}

	@Override
	public Integer getPlatformId() {
		return Platform.TYPE_TENCENT;
	}

	@Override
	OAuthProfile localize(String code) {
		String repString = doGet(genericUrl(urlGetToken, "client_id", client_id, "client_secret", client_secret,
				"grant_type", grant_type, "redirect_uri", redirect_uri, "code", code));
		String access_token = null;
		String remain = null;
		try {
			String[] params = repString.split("&");
			access_token = params[0].split("=")[1];
			remain = params[1].split("=")[1];
		} catch (Exception e) {
			log.error("QQ client get token error [{}]", repString);
			throw new BusinessException(E_TOKENJOERR);
		}
		return login(access_token, getOpenId(access_token), Integer.valueOf(remain));
	}

	@Override
	JSONObject getThirdPartyUser(String token, String uid) {
		return JSONObject.fromObject(doGet(genericUrl(urlGetUser, "access_token", token, "oauth_consumer_key",
				client_id, "openid", uid, "format", format)));
	}

	/**
	 * 获取QQ提供的用户UID
	 * 
	 * @param access_token
	 * @return 用户UID
	 */
	private String getOpenId(String access_token) {
		String repString[] = doGet(genericUrl(urlGetOpenId, "access_token", access_token)).split(" ");
		String result = "";
		if (repString.length > 1) {
			JSONObject jo = JSONObject.fromObject(repString[1]);
			if (jo.has("openid"))
				result = jo.getString("openid");
		}
		return result;
	}
}
